# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Ray Donnelly <mingw.android@gmail.com>

_realname=angleproject

pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}-git"
pkgver=1.3.r2070
pkgrel=1
pkgdesc='Angle project built from git source (mingw-w64)'
arch=('any')
url='http://code.google.com/p/angleproject/'
license=('LGPLv2+')
groups=('mingw-w64')
depends=()
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc" 'gyp-svn' 'git')
provides=("${MINGW_PACKAGE_PREFIX}-angleproject")
conflicts=("${MINGW_PACKAGE_PREFIX}-angleproject")
options=('!strip' 'staticlibs')
source=('angleproject::git+https://chromium.googlesource.com/angle/angle'
    D3DCompiler_43-i686.dll
    D3DCompiler_43-x86_64.dll
    'angleproject-include-import-library-and-use-def-file.patch'
    'angleproject-fix-mingw-compatibility.patch'
    'angleproject-export-shader-symbols.patch'
    'angleproject-fix-trace_event.patch'
    'angleproject-fix-renderer9-stencilformat.patch'
    '0050-Untangle-OS-from-GENERATOR-a-bit-for-MSYS2.patch'
    '0051-Move-event_tracer-.cpp-.h-to-libGLESv2-instead-of-co.patch'
    0001-Fix-compilation-for-MSVC-2008-and-std-tuple.patch
    0002-Fix-compilation-of-ANGLE-with-mingw-tdm64-gcc-4.8.1.patch
    0003-Fix-compilation-with-MinGW-gcc-64-bit.patch
    0004-Make-it-possible-to-link-ANGLE-statically-for-single.patch
    0005-Fix-build-when-SSE2-is-not-available.patch
    0006-Fix-compilation-of-libGLESv2-with-older-MinGW-w64-he.patch
    0007-Make-DX9-DX11-mutually-exclusive.patch
    0008-ANGLE-Dynamically-load-D3D-compiler-from-a-list-of-k.patch
    0009-ANGLE-Support-WinRT.patch
    0010-ANGLE-Enable-D3D11-for-feature-level-9-cards.patch
    0011-ANGLE-Fix-compilation-error-on-MinGW-caused-by-trace.patch
    0012-ANGLE-fix-semantic-index-lookup.patch
    0013-ANGLE-Allow-for-universal-program-binaries.patch
    0014-ANGLE-D3D11-Fix-internal-index-buffer-for-level-9-ha.patch)
md5sums=('SKIP'
         '1c9b45e87528b8bb8cfa884ea0099a85'
         'ada0c39d4eacdc81fd84163a95d62079'
         '4bf401fa460cc60802106fa6646b10df'
         '10b197b483a543fcf7e1ae5ec379848f'
         'be79402d4cf03e26f467d9f738823d3a'
         '59a3d60f5b1b6676056444db8a7883fe'
         '5f30f3a3c668e4a76920a71e0dddc87d'
         'd680608c3826af8ed61292ed693a6776'
         '992b68a3e691467e8f067d2a01cc5e8a'
         'c690dcdc1940cdba4a10f5509bbdbde5'
         '743518f9c30ce102bf9db07ff4b04c5b'
         'a9a3412a013f792a170181a370807ce0'
         '53b6de972d6827dced411ced3a748980'
         '701479de7b6c9e5e6261d795d989996d'
         '671f29fa7abd9eadd2bcc8d7e0e34b5d'
         'b49eb1e053587dd356a67c8c7318ef1e'
         'b08b19bb0a562cb16442412d5d664277'
         '45cc38b7271bfed011dd40999dd8d12b'
         '8d80596af43f645be9d5114435c4706e'
         '4d7b3d22a71973b2dd552367ffb483b0'
         'e103408c432bde88659b8f4618db32d1'
         '357849298a8b1ebfa73a9fa6648a12b5'
         '5faf59851fda5052282dbaf34602f778')
pkgver() {
  cd "$srcdir/angleproject"
  local _major=$(head -n 4 src/common/version.h | grep 'ANGLE_MAJOR_VERSION' | sed -e 's/.* //' | tr '\n' '.' | sed 's/.$/\n/')
  local _minor=$(head -n 4 src/common/version.h | grep 'ANGLE_MINOR_VERSION' | sed -e 's/.* //' | tr '\n' '.' | sed 's/.$/\n/')
  printf "%s.%s.r%s" "$_major" "$_minor" "$(git rev-list --count HEAD)"
}

build() {
  cd "${srcdir}"/angleproject

  export PYTHON=/usr/bin/python2
  # Make sure an import library is created and the correct .def file is used during the build
  patch -p0 -i "${srcdir}"/angleproject-include-import-library-and-use-def-file.patch

  # .def pieces taken from http://qt.gitorious.org/qt/qtbase/commit/b2c44985e740d0a0ea86b7b9f4cc849258ac2340
  patch -p0 -i "${srcdir}"/angleproject-fix-mingw-compatibility.patch

  # WebKit depends on symbols which are used in the static library called translator
  # This static library is linked into the libGLESv2 shared library
  # To allow building WebKit export the required symbols in the libGLESv2 shared library
  patch -p0 -i "${srcdir}"/angleproject-export-shader-symbols.patch

  # Fix missing methods in trace_event.h
  patch -p0 -i "${srcdir}"/angleproject-fix-trace_event.patch

  # Fix stencil format method in renderer9_utils.cpp
  patch -p0 -i "${srcdir}"/angleproject-fix-renderer9-stencilformat.patch

  # Untangle OS and GENERATOR some.
  patch -p1 -i "${srcdir}"/0050-Untangle-OS-from-GENERATOR-a-bit-for-MSYS2.patch

  # Fix multiply defined 'SetTraceFunctionPointers'
  patch -p1 -i "${srcdir}"/0051-Move-event_tracer-.cpp-.h-to-libGLESv2-instead-of-co.patch
  
  # Patches from Qt5
  patch -p4 -i "${srcdir}"/0001-Fix-compilation-for-MSVC-2008-and-std-tuple.patch
  patch -p4 -i "${srcdir}"/0002-Fix-compilation-of-ANGLE-with-mingw-tdm64-gcc-4.8.1.patch
  #patch -p4 -i "${srcdir}"/0003-Fix-compilation-with-MinGW-gcc-64-bit.patch
  #patch -p4 -i "${srcdir}"/0004-Make-it-possible-to-link-ANGLE-statically-for-single.patch
  patch -p4 -i "${srcdir}"/0005-Fix-build-when-SSE2-is-not-available.patch
  #patch -p4 -i "${srcdir}"/0006-Fix-compilation-of-libGLESv2-with-older-MinGW-w64-he.patch
  #patch -p4 -i "${srcdir}"/0007-Make-DX9-DX11-mutually-exclusive.patch
  #patch -p4 -i "${srcdir}"/0008-ANGLE-Dynamically-load-D3D-compiler-from-a-list-of-k.patch
  #patch -p4 -i "${srcdir}"/0009-ANGLE-Support-WinRT.patch
  patch -p4 -i "${srcdir}"/0010-ANGLE-Enable-D3D11-for-feature-level-9-cards.patch
  #patch -p4 -i "${srcdir}"/0011-ANGLE-Fix-compilation-error-on-MinGW-caused-by-trace.patch
  patch -p4 -i "${srcdir}"/0012-ANGLE-fix-semantic-index-lookup.patch
  #patch -p4 -i "${srcdir}"/0013-ANGLE-Allow-for-universal-program-binaries.patch
  #patch -p4 -i "${srcdir}"/0014-ANGLE-D3D11-Fix-internal-index-buffer-for-level-9-ha.patch

  export CFLAGS="-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions --param=ssp-buffer-size=4"
  export CXXFLAGS="-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions --param=ssp-buffer-size=4"
  
  sed -i -e 's_python _python2 _g' -e 's_"python"_"python2"_g' -e "s_'python'_'python2'_g" -e 's_/usr/bin/python_/usr/bin/python2_g' $(find -type f)
  sed -i -e 's|vsprintf_s|vsprintf|g' $(find . \( -name '*.h' -or -name '*.cpp' \) -and -type f)

  mkdir -p build-${MINGW_CHOST} && pushd build-${MINGW_CHOST}

  if [ "${MINGW_CHOST}" = "i686-w64-mingw32" ]
  then
    _target="win32"
  else
    _target="win64"
  fi

  gyp -D OS=win --format make -D TARGET=$_target --depth . -I ../build/common.gypi ../src/build_angle.gyp

  # Fix linking of libtranslator.a
  sed -i \
    -e s@'$(obj).target/$(TARGET)/../src/common/event_tracer.o'@@g \
    -e s@'$(obj).target/$(TARGET)/../src/common/RefCountObject.o'@@g \
    -e s@'$(obj).target/$(TARGET)/../src/common/debug.o'@@g \
    "${srcdir}"/angleproject/src/translator{,_static}.target.mk

  # LINK=g++ is to prevent using flock to serialize linking.
  LINK=g++ make -j1 V=1 CXXFLAGS="-std=c++11 -msse2 -DUNICODE -D_UNICODE"

  ${MINGW_PREFIX}/bin/ar rcs libGLESv2.a \
    out/Debug/obj.target/libGLESv2/../src/common/*.o \
    out/Debug/obj.target/libGLESv2/../src/libGLESv2/renderer/*.o \
    out/Debug/obj.target/libGLESv2/../src/libGLESv2/renderer/d3d9/*.o \
    out/Debug/obj.target/libGLESv2/../src/libGLESv2/renderer/d3d11/*.o \
    out/Debug/obj.target/libGLESv2/../src/libGLESv2/*.o \
    #out/Debug/obj.target/../src/libtranslator.a \
    #out/Debug/obj.target/../src/libpreprocessor.a
  ${MINGW_PREFIX}/bin/ar rcs libEGL.a \
    out/Debug/obj.target/libEGL/../src/common/*.o \
    out/Debug/obj.target/libEGL/../src/libEGL/*.o

  popd
}

package() {
  cd "${srcdir}"/angleproject
  pushd build-${MINGW_CHOST}

  mkdir -p "${pkgdir}${MINGW_PREFIX}"/{bin,lib,include}

  install out/Debug/src/libGLESv2.dll "${pkgdir}${MINGW_PREFIX}"/bin/libGLESv2.dll
  install out/Debug/src/libEGL.dll "${pkgdir}${MINGW_PREFIX}"/bin/libEGL.dll

  ${MINGW_PREFIX}/bin/strip --strip-unneeded "${pkgdir}${MINGW_PREFIX}"/bin/*.dll

  install libGLESv2.a "${pkgdir}${MINGW_PREFIX}"/lib/
  install libEGL.a "${pkgdir}${MINGW_PREFIX}"/lib/

  ${MINGW_PREFIX}/bin/strip --strip-debug "${pkgdir}${MINGW_PREFIX}"/lib/libEGL.a
  install libEGL.dll.a "${pkgdir}${MINGW_PREFIX}"/lib/
  install libGLESv2.dll.a "${pkgdir}${MINGW_PREFIX}"/lib/
  ${MINGW_PREFIX}/bin/strip --strip-unneeded "${pkgdir}${MINGW_PREFIX}"/lib/*.dll.a

  cp -Rv ../include/* "${pkgdir}${MINGW_PREFIX}"/include/

  popd

  install -Dm755 ${srcdir}/D3DCompiler_43-${CARCH}.dll "${pkgdir}${MINGW_PREFIX}"/bin/D3DCompiler_43.dll
}
